﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.ObjectMapping;
using WMSSystem.DYB.Dto.Inboundmanagement.Input;
using WMSSystem.DYB.Dto.Inboundmanagement.Output;
using WMSSystem.DYB.Interface;
using WMSSystem.WYJ;
using WMSSystem.YJ;
using WMSSystem.YSL.Interfance;
using WMSSystem.Application.Contracts.Workflow;
using WMSSystem.Application.Contracts.Workflow.Dto;
using WMSSystem.Application.Workflow;

namespace WMSSystem.DYB.Application
{
    public class InboundmanagementService:ApplicationService,IInboundmanagementService
    {
        private readonly IRepository<Inboundmanagement,Guid> _inboundmanagementRepository;
        private readonly ICodeRuleAppService _codeRuleAppService;
        private readonly IRepository<InventoryDetails,Guid> _inventoryRepositroy;
        private readonly IRepository<QuantityList,Guid> _quantityListRepositroy;
        private readonly IRepository<InventoryGoods,Guid> _inventoryGoodsRepositroy;
        private readonly IRepository<WYJ_Buyin,Guid> _buyinRepositroy;
        private readonly IRepository<Commoditiestable, Guid> _commoditiesRepository;
        private readonly IRepository<LocationManagement, Guid> _locationManagementRepository;
        private readonly IRepository<WarehouseManagement, Guid> _warehouseManagementRepository;
        private readonly IRepository<IdentityUser, Guid> _userRepositroy;
        private readonly IWorkflowService _workflowService;
        private readonly UserRoleService _userRoleService;
        private readonly IRepository<PendingApproval, Guid> _pendingApprovalRepository;
        public InboundmanagementService(IRepository<Inboundmanagement, Guid> inboundmanagementRepository, ICodeRuleAppService codeRuleAppService, IRepository<InventoryDetails, Guid> inventoryRepositroy, IRepository<QuantityList, Guid> quantityListRepositroy, IRepository<InventoryGoods, Guid> inventoryGoodsRepositroy, IRepository<WYJ_Buyin, Guid> buyinRepositroy, IRepository<Commoditiestable, Guid> commoditiesRepository, IRepository<LocationManagement, Guid> locationManagementRepository, IRepository<WarehouseManagement, Guid> warehouseManagementRepository, IRepository<IdentityUser, Guid> userRepositroy, IWorkflowService workflowService, UserRoleService userRoleService, IRepository<PendingApproval, Guid> pendingApprovalRepository)
        {
            _inboundmanagementRepository = inboundmanagementRepository;
            _codeRuleAppService = codeRuleAppService;
            _inventoryRepositroy = inventoryRepositroy;
            _quantityListRepositroy = quantityListRepositroy;
            _inventoryGoodsRepositroy = inventoryGoodsRepositroy;
            _buyinRepositroy = buyinRepositroy;
            _commoditiesRepository = commoditiesRepository;
            _locationManagementRepository = locationManagementRepository;
            _warehouseManagementRepository = warehouseManagementRepository;
            _userRepositroy = userRepositroy;
            _workflowService = workflowService;
            _userRoleService = userRoleService;
            _pendingApprovalRepository = pendingApprovalRepository;
        }
        /// <summary>
        /// 添加入库单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiResponse<int>> CreateInbound(CreateInboundInput input)
        {
            try
            {
                //入库货品，入库数量，入库金额
                var inbound = ObjectMapper.Map<CreateInboundInput, Inboundmanagement>(input);
                inbound.Code = await _codeRuleAppService.GenerateCodeAsync("inbound");
                
                inbound.State = 2;
                
                List<InventoryDetails> inventoryDetails = new List<InventoryDetails>();
                List<LocationManagement> locationManagements = new List<LocationManagement>();
                List<QuantityList> quantityLists = new List<QuantityList>();
                foreach (var item in input.CreateInventoryDetailsInput)
                {
                    var inventorydetailmodel = ObjectMapper.Map<CreateInventoryDetailsInput, InventoryDetails>(item);
                    inventorydetailmodel.InboundmanagementCode = inbound.Code;
                    inventorydetailmodel.CommoditiesId = item.CommoditiesId;
                    var buyin = await _buyinRepositroy.GetAsync(x=>x.Id==inbound.Inventorydetailsid);
                    if (buyin == null)
                    {
                        throw new Exception("关联单号不存在，请检查");
                    }
                    var detail = await _quantityListRepositroy.GetAsync(x => x.BuYinId == buyin.Id && x.CommoditiesId==item.CommoditiesId);
                    if (detail == null)
                    {
                        throw new Exception("关联单号不存在，请检查");
                    }
                    inventorydetailmodel.Quantitylistid = detail.Id;
                    var good = await _inventoryGoodsRepositroy.GetAsync(x=>x.CommoditiesId == item.CommoditiesId && x.PositionManagementId==inbound.Warehouseid);
                    inventorydetailmodel.BefareNum = good.Inventory; //入库前库存
                    if(item.Num <=0)
                    {
                        throw new Exception("入库数量必须大于0");
                    }
                    if(item.Num > detail.UnstockedQuantity)
                    {
                        throw new Exception("入库数量不能大于未库存数量");
                    }
                    inventoryDetails.Add(inventorydetailmodel);

                    var commodity = await _commoditiesRepository.GetAsync(x => x.Id == item.CommoditiesId);

                    if(inbound.Price==null)
                    {
                        inbound.Price = 0;
                    }
                    if (inbound.Num == null)
                    {
                        inbound.Num = 0;
                    }

                    inbound.Price = inbound.Price + (decimal)(item.Num * commodity.InhomeMoney); //入库金额
                    inbound.Num = inbound.Num + item.Num; //入库数量
                    quantityLists.Add(detail);
                    var goos = new string[] { };
                    if (!string.IsNullOrEmpty(inbound.Inboundgoods))
                    {
                        goos = inbound.Inboundgoods.Split(',');
                    }
                    
                    if (!goos.Contains(commodity.CommoditiesName))
                    {
                        if(inbound.Inboundgoods == null)
                        {
                            inbound.Inboundgoods = commodity.CommoditiesName; //入库货品
                        }
                        else
                        {
                            inbound.Inboundgoods += "," + commodity.CommoditiesName; //入库货品
                        }    
                    }

                    //var location = await _locationManagementRepository.GetAsync(x=>x.Id == item.Positionid);
                    //var locat = new string[] { };
                    //if (!string.IsNullOrEmpty(location.StoreItems))
                    //{
                    //    locat = location.StoreItems.Split(',');
                    //}

                    //if (!locat.Contains(commodity.CommoditiesName))
                    //{
                    //    if (location.StoreItems == null)
                    //    {
                    //        location.StoreItems = commodity.CommoditiesName; //入库货品
                    //    }
                    //    else
                    //    {
                    //        location.StoreItems += "," + commodity.CommoditiesName; //入库货品
                    //    }
                    //}
                    //locationManagements.Add(location);
                }

                await _locationManagementRepository.UpdateManyAsync(locationManagements);
                await _inventoryRepositroy.InsertManyAsync(inventoryDetails);
                await _inboundmanagementRepository.InsertAsync(inbound);
                await _quantityListRepositroy.UpdateManyAsync(quantityLists);

                if (inbound.State == 1)
                {
                    return new ApiResponse<int>()
                    {
                        Code = ErrorCode.Success,
                        Message = "添加草稿成功",
                        Data = 1
                    };
                }
                if(inbound.State==2)
                {
                        return new ApiResponse<int>()
                        {
                            Code = ErrorCode.Success,
                            Message = "入库待审批",
                            Data = 1
                        };
                }
                return new ApiResponse<int>()
                {
                    Code = ErrorCode.Success,
                    Message = "添加入库单成功",
                    Data = 1
                };
            }
            catch (Exception ex)
            {
                return new ApiResponse<int>
                {
                    Code = ErrorCode.Error,
                    Message = ex.Message,
                    Data = 0
                };
            }

            
        }
        /// <summary>
        /// 获取进货单列表
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiResponse<List<GetBuyinListOutput>>> GetBuyinList()
        {
            var query = await _buyinRepositroy.GetQueryableAsync();
            var output = from item in query
                         select new GetBuyinListOutput
                         {
                             Id = item.Id,
                             BuyinCode = item.BuyinCode,
                             BuyinStatus = item.BuyinStatus,
                             BuyDate = item.BuyDate,
                             BuyinDept = item.BuyinDept,
                             BuyinPrice = item.BuyinPrice,
                             BuyinManager = item.BuyinManager,
                             BuyinNumber = item.BuyinNumber,
                             PresetOne = item.PresetOne,
                             PresetThree = item.PresetThree,
                             PresetTwo = item.PresetTwo
                         };
           return new ApiResponse<List<GetBuyinListOutput>>
            {
                Code = ErrorCode.Success,
                Message = "获取进货单列表成功",
                Data = output.ToList()
            };
        }

        /// <summary>
        /// 获取入库单列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ApiPaggingResponse<GetInboundListOutput>> GetInboundList(GetInboundListInput input)
        {
            var query = await _inboundmanagementRepository.GetQueryableAsync();
            var warehouse = await _warehouseManagementRepository.GetQueryableAsync();
            var buyin = await _buyinRepositroy.GetQueryableAsync();
            var users = await _userRepositroy.GetQueryableAsync();
            //分页
            query = query.OrderByDescending(x=>x.Id).Skip((input.Index-1)*input.Size).Take(input.Size);

            var output = from item in query
                         join ware in warehouse on item.Warehouseid equals ware.Id
                         join buy in buyin on item.Inventorydetailsid equals buy.Id
                         join user in users on item.Manager equals user.Id
                         select new GetInboundListOutput
                         {
                             Id = item.Id,
                             Code = item.Code,
                             Type = item.Type,
                             State = item.State,
                             Date = item.Date,
                             WarehouseName = ware.Name,
                             InventorydetailsCode = buy.BuyinCode,
                             Inboundgoods = item.Inboundgoods,
                             Manager = user.UserName,
                             Num = item.Num,
                             Price = item.Price,
                             CreateName = "admin",
                             Phone = item.Phone,
                             Remark = item.Remark,
                             Contact = item.Contact,
                             Reviewer = "",
                             //返回一个空的审核时间
                             ReviewTime = null,
                             Vendor = item.Vendor,
                             CreateionTime = DateTime.Now,
                         };

            var totalCount = output.Count();
            return new ApiPaggingResponse<GetInboundListOutput>
            {
                TotalCount = totalCount,
                PageDatas = output.ToList(),
                Code = ErrorCode.Success,
                Message = "获取入库单列表成功"
            };
        }
        /// <summary>
        /// 获取进货单详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<GetQuantityListOutput>> GetQuantityList(Guid id)
        {
            var quantitys = await _quantityListRepositroy.GetQueryableAsync();
            var commodities = await _commoditiesRepository.GetQueryableAsync();
            quantitys = quantitys.Where(x=>x.BuYinId == id);
            if(!quantitys.Any())
            {
                return new List<GetQuantityListOutput>();
            }
            var output = from a in quantitys
                         join b in commodities
                         on a.CommoditiesId equals b.Id
                         select new GetQuantityListOutput
                         {
                             CommoditiesCode = b.CommoditiesCode,
                             CommoditiesGuike = b.CommoditiesGuike,
                             QuantityStored = a.QuantityStored,
                             CommoditiesId = b.Id,
                             CommoditiesName = b.CommoditiesName,
                             CommoditiesUnit = b.CommoditiesUnit,
                             InhomeMoney = b.InhomeMoney,
                             Inventory = a.Inventory,
                             UnstockedQuantity = a.UnstockedQuantity,
                         };
            return output.ToList();
        }

        /// <summary>
        /// 审批入库单
        /// </summary>
        /// <param name="input">审批输入</param>
        /// <returns>审批结果</returns>
        public async Task<ApiResponse<bool>> ApproveInbound(ApproveInboundInput input)
        {
            try
            {
                // 获取当前登录用户（支持自定义用户系统）
                var currentUserId = CurrentUser.Id.Value;
                var currentUser = await _userRepositroy.GetAsync(currentUserId);
                
                // 如果 ABP Identity 中没有用户，尝试从自定义系统获取
                if (currentUser == null)
                {
                    // 这里可以添加自定义用户系统的支持
                    // 例如：从 UserLogin 表获取用户信息
                    return new ApiResponse<bool>
                    {
                        Code = ErrorCode.Error,
                        Message = "用户信息不存在",
                        Data = false
                    };
                }

                // 获取入库单
                var inbound = await _inboundmanagementRepository.GetAsync(input.InboundId);
                if (inbound == null)
                {
                    return new ApiResponse<bool>
                    {
                        Code = ErrorCode.Error,
                        Message = "入库单不存在",
                        Data = false
                    };
                }

                // 获取工作流实例
                var workflowResult = await _workflowService.GetWorkflowInstanceByBusinessAsync("Inbound", input.InboundId);
                if (workflowResult.Code != ErrorCode.Success)
                {
                    return new ApiResponse<bool>
                    {
                        Code = ErrorCode.Error,
                        Message = "工作流实例不存在",
                        Data = false
                    };
                }

                // 根据当前用户角色确定审批步骤
                var stepName = await _userRoleService.DetermineApprovalStepAsync(currentUser.Id);
                if (string.IsNullOrEmpty(stepName))
                {
                    return new ApiResponse<bool>
                    {
                        Code = ErrorCode.Error,
                        Message = "当前用户没有审批权限",
                        Data = false
                    };
                }

                // 执行审批
                var approvalInput = new WorkflowApprovalDto
                {
                    WorkflowInstanceId = Guid.Parse(workflowResult.Data.WorkflowInstanceId),
                    ApproverId = currentUser.Id.ToString(),
                    ApprovalResult = input.ApprovalResult,
                    ApprovalComment = input.ApprovalComment,
                    StepName = stepName
                };

                var approvalResult = await _workflowService.ApproveWorkflowAsync(approvalInput);
                if (approvalResult.Code != ErrorCode.Success)
                {
                    return new ApiResponse<bool>
                    {
                        Code = ErrorCode.Error,
                        Message = $"审批失败: {approvalResult.Message}",
                        Data = false
                    };
                }

                // 更新入库单状态
                if (input.ApprovalResult == "Approved")
                {
                    // 根据审批步骤更新状态
                    switch (stepName)
                    {
                        case "ManagerApproval":
                            inbound.State = 2; // 经理审批通过，等待财务审批
                            break;
                        case "FinanceApproval":
                            inbound.State = 2; // 财务审批通过，等待仓库管理员审批
                            break;
                        case "WarehouseManagerApproval":
                            inbound.State = 4; // 所有审批通过，已完成
                            // 所有审批通过后，执行入库操作
                            await ExecuteInboundOperationAsync(inbound);
                            break;
                    }
                }
                else
                {
                    inbound.State = 3; // 审批被拒绝，状态为驳回
                }

                await _inboundmanagementRepository.UpdateAsync(inbound);

                return new ApiResponse<bool>
                {
                    Code = ErrorCode.Success,
                    Message = "审批成功",
                    Data = true
                };
            }
            catch (Exception ex)
            {
                return new ApiResponse<bool>
                {
                    Code = ErrorCode.Error,
                    Message = ex.Message,
                    Data = false
                };
            }
        }

        /// <summary>
        /// 获取待审批的入库单列表
        /// </summary>
        /// <param name="approverId">审批人ID</param>
        /// <returns>待审批列表</returns>
        public async Task<ApiResponse<List<GetInboundListOutput>>> GetPendingApprovalInboundList(Guid approverId)
        {
            try
            {
                var pendingWorkflows = await _workflowService.GetPendingApprovalListAsync(approverId);
                if (pendingWorkflows.Code != ErrorCode.Success)
                {
                    return new ApiResponse<List<GetInboundListOutput>>
                    {
                        Code = ErrorCode.Error,
                        Message = "获取待审批列表失败",
                        Data = new List<GetInboundListOutput>()
                    };
                }

                var inboundIds = pendingWorkflows.Data
                    .Where(w => w.BusinessType == "Inbound")
                    .Select(w => w.BusinessId)
                    .ToList();

                if (!inboundIds.Any())
                {
                    return new ApiResponse<List<GetInboundListOutput>>
                    {
                        Code = ErrorCode.Success,
                        Message = "暂无待审批的入库单",
                        Data = new List<GetInboundListOutput>()
                    };
                }

                var query = await _inboundmanagementRepository.GetQueryableAsync();
                var output = from item in query
                            where inboundIds.Contains(item.Id)
                            select new GetInboundListOutput
                            {
                                Id = item.Id,
                                Code = item.Code,
                                Type = item.Type,
                                State = item.State,
                                Date = item.Date,
                                WarehouseName = "", // 需要从仓库表获取
                                InventorydetailsCode = item.Inventorydetailsid.ToString(),
                                Inboundgoods = item.Inboundgoods,
                                Manager = item.Manager.ToString(),
                                Num = item.Num,
                                Price = item.Price,
                                Remark = item.Remark
                            };

                return new ApiResponse<List<GetInboundListOutput>>
                {
                    Code = ErrorCode.Success,
                    Message = "获取待审批列表成功",
                    Data = output.ToList()
                };
            }
            catch (Exception ex)
            {
                return new ApiResponse<List<GetInboundListOutput>>
                {
                    Code = ErrorCode.Error,
                    Message = ex.Message,
                    Data = new List<GetInboundListOutput>()
                };
            }
        }

        /// <summary>
        /// 执行入库操作（所有审批通过后调用）
        /// </summary>
        /// <param name="inbound">入库单</param>
        private async Task ExecuteInboundOperationAsync(Inboundmanagement inbound)
        {
            try
            {
                // 获取入库明细
                var inventoryDetails = await _inventoryRepositroy.GetListAsync(x => x.InboundmanagementCode == inbound.Code);
                
                foreach (var detail in inventoryDetails)
                {
                    // 更新库存
                    var inventoryGoods = await _inventoryGoodsRepositroy.GetAsync(x => x.CommoditiesId == detail.CommoditiesId);
                    if (inventoryGoods != null)
                    {
                        inventoryGoods.Inventory += detail.Num; // 增加库存
                        await _inventoryGoodsRepositroy.UpdateAsync(inventoryGoods);
                    }
                    
                    // 更新数量列表
                    var quantityList = await _quantityListRepositroy.GetAsync(x => x.Id == detail.Quantitylistid);
                    if (quantityList != null)
                    {
                        quantityList.QuantityStored += detail.Num; // 已入库数量
                        quantityList.UnstockedQuantity -= detail.Num; // 未入库数量
                        await _quantityListRepositroy.UpdateAsync(quantityList);
                    }
                }
                
                // 记录操作日志
                // await _logService.LogAsync("入库操作", $"入库单 {inbound.Code} 审批通过，库存已更新");
            }
            catch (Exception ex)
            {
                // 记录错误日志
                // await _logService.LogErrorAsync("入库操作失败", ex.Message);
                throw new Exception($"执行入库操作失败: {ex.Message}");
            }
        }
        /// <summary>
        /// 审批入库单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ApiResponse<int>> AuditInbound(AuditInput input)
        {
            var inbound = await _inboundmanagementRepository.GetAsync(x=>x.State==2 && x.Id==input.Id);

            var details = await _inventoryRepositroy.GetListAsync(x => x.InboundmanagementCode == inbound.Code);
            List<LocationManagement> locationManagements = new List<LocationManagement>();
            foreach (var item in details)
            {
                var inventoryGoods = await _inventoryGoodsRepositroy.GetAsync(x => x.CommoditiesId == item.CommoditiesId && x.PositionManagementId==inbound.Warehouseid);
                if (inventoryGoods != null)
                {
                    if(inventoryGoods.PositionManagementId==inbound.Warehouseid)
                    {
                        inventoryGoods.Inventory += item.Num; // 增加库存
                        await _inventoryGoodsRepositroy.UpdateAsync(inventoryGoods);

                    }
                    else
                    {
                        InventoryGoods newInventoryGoods = new InventoryGoods
                        {
                            CommoditiesId = item.CommoditiesId,
                            Inventory = item.Num, // 新增库存数量
                            PositionManagementId = inbound.Warehouseid, // 设置仓库ID
                        };
                        await _inventoryGoodsRepositroy.InsertAsync(newInventoryGoods);
                    }
                }
                var quantityList = await _quantityListRepositroy.GetAsync(x => x.Id == item.Quantitylistid);
                if (quantityList != null)
                {
                    quantityList.QuantityStored += item.Num; // 已入库数量
                    quantityList.UnstockedQuantity -= item.Num; // 未入库数量
                    await _quantityListRepositroy.UpdateAsync(quantityList);
                }
                var location = await _locationManagementRepository.GetAsync(x => x.Id == item.Positionid);
                var locat = new string[] { };
                if (!string.IsNullOrEmpty(location.StoreItems))
                {
                    locat = location.StoreItems.Split(',');
                }
                var commodity = await _commoditiesRepository.GetAsync(x => x.Id == item.CommoditiesId);
                if (!locat.Contains(commodity.CommoditiesName))
                {
                    if (location.StoreItems == null)
                    {
                        location.StoreItems = commodity.CommoditiesName; //入库货品
                    }
                    else
                    {
                        location.StoreItems += "," + commodity.CommoditiesName; //入库货品
                    }
                }
                locationManagements.Add(location);
            }
            inbound.State = input.State; // 更新入库单状态
            await _locationManagementRepository.UpdateManyAsync(locationManagements);
            await _inboundmanagementRepository.UpdateAsync(inbound);
            if(inbound.State == 4)
            {
                return new ApiResponse<int>
                {
                    Code = ErrorCode.Success,
                    Message = "入库单审核通过",
                    Data = 1
                };
            }
            else
            {
                return new ApiResponse<int>
                {
                    Code = ErrorCode.Success,
                    Message = "入库单审核驳回",
                    Data = 0
                };
            }

        }
        /// <summary>
        /// 获取入库明细
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public async Task<List<GetdetailsListOutput>> GetdetailsList(string code)
        {
            var query = await _inventoryRepositroy.GetQueryableAsync();
            query = query.Where(x=>x.InboundmanagementCode == code);
            var commodities = await _commoditiesRepository.GetQueryableAsync();
            var locations = await _locationManagementRepository.GetQueryableAsync();
            var output = from item in query
                         join commodity in commodities on item.CommoditiesId equals commodity.Id
                         join location in locations on item.Positionid equals location.Id
                         select new GetdetailsListOutput
                         {
                             LocationName = location.Name,
                             BefareNum = item.BefareNum,
                             AfterNum = item.BefareNum + item.Num, //入库后库存
                             Num = item.Num,
                             Lotnumber = item.Lotnumber,
                             Date = item.Date,
                             Remark = item.Remark,
                             CommoditiesName = commodity.CommoditiesName,
                             CommoditiesCode = commodity.CommoditiesCode,
                             CommoditiesGuike = commodity.CommoditiesGuike,
                             CommoditiesType = commodity.CommoditiesType,
                             CommoditiesUnit = commodity.CommoditiesUnit,
                             InhomeMoney = commodity.InhomeMoney
                         };
            return output.ToList();
        }
        /// <summary>
        /// 获取入库单详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<GetInboundOutput> GetInboundmanagementByIdAsync(Guid id)
        {
            var query = await _inboundmanagementRepository.GetAsync(id);
            if (query == null)
            {
                throw new Exception("入库单不存在");
            }
            var warehouse = await _warehouseManagementRepository.GetAsync(query.Warehouseid);
            var buyin = await _buyinRepositroy.GetAsync(x => x.Id == query.Inventorydetailsid);
            var user = await _userRepositroy.GetAsync(x => x.Id == query.Manager);
            return new GetInboundOutput
            {
                Code = query.Code,
                Type = query.Type,
                State = query.State,
                Date = query.Date,
                Inboundgoods = query.Inboundgoods,
                Phone = user.PhoneNumber, // 假设这里是电话
                Remark = query.Remark, // 假设这里是备注
                Contact = query.Contact, // 假设这里是联系人
                Vendor = query.Vendor, // 假设这里是供应商
                InventorydetailsCode = buyin.BuyinCode, // 假设这里是关联单号
                Manager = user.UserName, // 假设这里是经办人
                Num = query.Num, // 假设这里是入库数量
                Price = query.Price, // 假设这里是入库金额
                WarehouseName = warehouse.Name, // 假设这里是仓库名称
            };
        }
    }
}
